﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessTier.Facade.Interface;
using BusinessTier.Facade.Implementation;

namespace WebMVC.Areas.AdminPanel.Controllers
{
    public class DataController : Controller
    {
        private IProductService productService;
        

        public DataController() : this(new ProductService())
        {
            
        }

        public DataController(IProductService productService)
        {
            this.productService = productService;
        }
        public ActionResult Index()
        {
            return View(productService.GetAllPrintMedias());
        }

        public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
        {
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = context.Questions.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var questions = context.Questions.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);

            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = (
                    from question in questions
                    select new
                    {
                        i = question.Id,
                        cell = new string[] { question.Id.ToString(), question.Votes.ToString(), question.Title }
                    }).ToArray()
            };
            return Json(jsonData);
        }

    }
}
